package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.util.AuthContextUtil;
import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysMenuRoleMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName: SysMenuServiceImpl
 * @Package: com.atguigu.spzx.manager.service.impl
 * @Description 菜单管理功能的业务处理
 * @Author 孤妤
 * @Date: 2023/12/6 9:50
 */
@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysMenuRoleMapper sysMenuRoleMapper;
    @Override
    @Transactional(readOnly = true)
    /**
     * @description:查询菜单列表并转成树形结构
     * @param: []
     * @return: java.util.List<com.atguigu.spzx.model.entity.system.SysMenu>
     **/
    public List<SysMenu> findAllSysMenu() {
        List<SysMenu> sysMenu = sysMenuMapper.selectAllSysMenu();
        List<SysMenu> sysMenus = MenuHelper.buildTree(sysMenu);
        return sysMenus;
    }

    @Override
    /**
     * @description:添加主子节点,根据前端传过来的parentId决定添加主节点还是子节点
     * @param: [sysMenu]
     * @return: void
     **/
    public void addSysMenu(SysMenu sysMenu) {
        sysMenuMapper.insertSysMenu(sysMenu);
        updateSysRoleMenuIsHalf(sysMenu) ;
    }
    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {

        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if(parentMenu != null) {

            // 将该id的菜单设置为半开
            sysMenuRoleMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;

        }

    }

    @Override
    @Transactional(readOnly = true)
    /**
     * @description:
     * @param: 根据id查询菜单信息
     * @return: com.atguigu.spzx.model.entity.system.SysMenu
     **/
    public SysMenu findSysMenuById(Long id) {
        return sysMenuMapper.selectSysMenuById(id);
    }

    @Override
    /**
     * @description: 修改菜单信息
     * @param: [sysMenu]
     * @return: void
     **/
    public void changeSysMenu(SysMenu sysMenu) {
        sysMenuMapper.updateSysMenu(sysMenu);
    }

    @Override
    /**
     * @description: 根据id删除菜单
     * @param: [id]
     * @return: void
     **/
    public void removeSysMenuById(Long id) {
        sysMenuMapper.deleteSysMenuById(id);
    }

    @Override
    /**
     * @description: 根据角色id查看角色的菜单信息,用于菜单数据回显
     * @param: [id]
     * @return: java.util.List<java.lang.Long>
     **/
    public List<Long> findSysMenuEcho(Long id) {
        return sysMenuMapper.selectSysMenuEcho(id);
    }

    @Override
    @Transactional(readOnly = true)
      /**
       * @description: 根据当前用户id获取角色对应的菜单
       * @param: []
       * @return: java.util.List<com.atguigu.spzx.model.vo.system.SysMenuVo>
       **/
    public List<SysMenuVo> findUserMenuList() {
        // 获取当前用户
        SysUser sysUser = AuthContextUtil.get();
        // 获取当前用户的id
        Long id = sysUser.getId();
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(id);
        //构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        return this.buildMenus(sysMenuTreeList);

    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
